package edu.purdue.spm.component;
 
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.concurrent.ExecutionException;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemSelectedListener;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.Spinner;
import android.widget.TimePicker;
import android.widget.Toast;
import edu.purdue.spm.GroupDashBoardFragment;
import edu.purdue.spm.Login;
import edu.purdue.spm.R;
import edu.purdue.spm.util.Portal;

public class CreateGroupTaskPanelFragment extends Fragment{

	static View base;
	DatePicker dp;
	TimePicker tp;
	EditText task_title;
	Spinner group_member_spinner;
	EditText task_detail;
	Button confirm;
	Button discard;

	String group_member_list[];

	@Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
		Log.i("MSG", "CreateGroupTaskPanelFragment onCreateView");
		base = inflater.inflate(R.layout.create_new_group_task_panel, container, false);
		
		//Task title
		task_title = (EditText) base.findViewById(R.id.group_task_title_textfield);
		
		//Group member spinner
		group_member_spinner = (Spinner) base.findViewById(R.id.spinner1);
		group_member_list = GroupDashBoardFragment.group_members;
		setDropDownMenuListner();
		
		//DatePicer, cut off the calendar view
		dp = (DatePicker) base.findViewById(R.id.datePicker1);
		dp.setCalendarViewShown(false);
		tp = (TimePicker) base.findViewById(R.id.group_task_due_timePicker);
		
		//Task details
		task_detail = (EditText) base.findViewById(R.id.group_task_detail_TextField);
		
		//Confirm and Discard Buttons
		confirm = (Button) base.findViewById(R.id.confirm_new_group_task_Button);
		discard = (Button) base.findViewById(R.id.discard_new_group_task_Button);
		setConfirmButtonListner();
		setDiscardButtonListner();
		
        return base;
    }


	
	public void setConfirmButtonListner(){
		confirm.setOnClickListener(new Button.OnClickListener(){
			@Override
			public void onClick(View v) {
				if(task_title.getText().toString().isEmpty()) {
        	        Toast.makeText(base.getContext(), "Task Title cannot be empty.", Toast.LENGTH_LONG).show();
				} else if (task_detail.getText().toString().isEmpty()){
        	        Toast.makeText(base.getContext(), "Task Details cannot be empty.", Toast.LENGTH_LONG).show();
				} else {
        	        //Toast.makeText(base.getContext(), "Task Created!", Toast.LENGTH_LONG).show();
					String owner_id = group_member_list[group_member_spinner.getSelectedItemPosition()];
					String title = task_title.getText().toString();
					String task_content = task_detail.getText().toString();
					
					Date date1= (Date) new Date(dp.getYear()-1900, dp.getMonth(), dp.getDayOfMonth(), tp.getCurrentHour(), tp.getCurrentMinute());
					SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm");
					String due_time = sdf.format(date1);

					String task_weight = "1";
					String depth = "L";
					String group_id = GroupDashBoardFragment.group_ID[GroupDashBoardFragment.currentSelectedGroup];
					
					assignNewTask(owner_id, title, task_content, due_time, task_weight, depth, group_id);

				}
			}
		});
		
	}
	
	public void setDiscardButtonListner(){
		discard.setOnClickListener(new Button.OnClickListener(){
			@Override
			public void onClick(View v) {
				getFragmentManager().beginTransaction().remove(GroupDashBoardFragment.GrouptaskPanelFragment).commit();

			}
		});
	}
	
	public void setDropDownMenuListner(){
		ArrayAdapter<String> LTRadapter = new ArrayAdapter<String>(this.getActivity(), android.R.layout.simple_spinner_item, group_member_list);
		LTRadapter.setDropDownViewResource(android.R.layout.simple_dropdown_item_1line);
		group_member_spinner.setAdapter(LTRadapter);
		group_member_spinner.setOnItemSelectedListener(new OnItemSelectedListener() {

			@Override
			public void onItemSelected(AdapterView<?> arg0, View arg1,
					int position, long arg3) {
					
			}
			
			@Override
			public void onNothingSelected(AdapterView<?> arg0) {
					
			}	
			
		});
	}
	
	private void assignNewTask(String owner_id, String task_title, String task_content,
			String due_time, String task_weight, String depth, String group_id){

		System.out.println("Assigning new task.");
		try {
			String task_id = group_id+System.currentTimeMillis()/1000;
			Portal.message = "AddGroupTask:;:"+Login.GCMaccount+":;:"+
					task_id+":.:0:.:'"+owner_id+"':.:'"+task_title+
					"':.:'"+task_content+"':.:'"+due_time+"':.:0"+
					":.:'G':.:"+task_weight+":.:'"+depth+"':.:"+group_id;

			Portal.sendMessage(Portal.message);
			Portal.ReadMessages tmp = new Portal.ReadMessages();
			tmp.execute().get();

			//Toast.makeText(base.getContext(), Portal.messageBack, Toast.LENGTH_LONG).show();

			GroupDashBoardFragment.getGroupTaskIDs(GroupDashBoardFragment.group_ID[GroupDashBoardFragment.currentSelectedGroup]);
			GroupDashBoardFragment.getGroupTaskInfos(GroupDashBoardFragment.group_task_IDs);
			GroupDashBoardFragment.getTaskNames(GroupDashBoardFragment.group_task);
			setTaskList(GroupDashBoardFragment.group_task_name);
			
		} catch (InterruptedException e) {

			e.printStackTrace();
		} catch (ExecutionException e) {

			e.printStackTrace();
		}

	}
	
	private void setTaskList(String [] tasks) {
		ArrayAdapter<String> adapter = new ArrayAdapter<String>(this.getActivity(), R.layout.group_task_listview, tasks);
		GroupDashBoardFragment.taskList.setAdapter(adapter); 
	}
}